home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gs250.exe / BDFTOPS.PS < prev    next >
Encoding:
Text File  |  1992-09-03  |  23.1 KB  |  753 lines

  1. %    Copyright (C) 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % bdftops.ps
  21. % Convert a BDF file (possibly with (an) associated AFM file(s))
  22. % to a Ghostscript font.
  23.  
  24. % Ghostscript fonts are in the same format as Adobe Type 1 fonts,
  25. % except that they do not use eexec encryption.
  26. % See gs_fonts.ps for more information.
  27.  
  28. /envBDF 120 dict def
  29. envBDF begin
  30.  
  31. % "Import" the image-to-path package.
  32. % This also brings in the Type 1 opcodes (type1ops.ps).
  33.    (impath.ps) run
  34.  
  35. % "Import" the font-writing package.
  36.    (wrfont.ps) run
  37.  
  38. % Invert the StandardEncoding vector.
  39.    256 dict dup begin
  40.    0 1 255 { dup StandardEncoding exch get exch def } for
  41.    end /decoding exch def
  42.  
  43. % Define the dictionary equivalent of ].
  44.    /dicttomark
  45.     { counttomark 2 idiv dup dict begin
  46.        { def } repeat
  47.       pop currentdict end 
  48.     } bind def
  49.  
  50. % Define the properties copied to FontInfo.
  51.    mark
  52.      (COPYRIGHT) /Notice
  53.      (FAMILY_NAME) /FamilyName
  54.      (FULL_NAME) /FullName
  55.      (WEIGHT_NAME) /Weight
  56.    dicttomark /properties exch def
  57.  
  58. % Define the character sequences used to fill in some undefined entries
  59. % in the standard encoding.
  60.    mark
  61.      (exclamdown) [/exclam]
  62.      (fraction) [/slash]
  63.      (florin) [/f]
  64.      (quotesingle) [/quoteright]
  65.      (quotedblleft) [/quotedbl]
  66.      (guillemotleft) [/less /less]
  67.      (guilsinglleft) [/less]
  68.      (guilsinglright) [/greater]
  69.      (fi) [/f /i]
  70.      (fl) [/f /l]
  71.      (endash) [/hyphen /hyphen]
  72.      (periodcentered) [/asterisk]
  73.      (bullet) [/asterisk]
  74.      (quotesinglbase) [/quotesingle]
  75.      (quotedblbase) [/quotedbl]
  76.      (quotedblright) [/quotedbl]
  77.      (guillemotright) [/greater /greater]
  78.      (ellipsis) [/period /period /period]
  79.      (questiondown) [/question]
  80.      (grave) [/quoteleft]
  81.      (acute) [/quoteright]
  82.      (circumflex) [/asciicircum]
  83.      (tilde) [/asciitilde]
  84.      (dieresis) [/quotedbl]
  85.      (cedilla) [/comma]
  86.      (hungarumlaut) [/quotedbl]
  87.      (emdash) [/hyphen /hyphen /hyphen]
  88.      (AE) [/A /E]
  89.      (OE) [/O /E]
  90.      (ae) [/a /e]
  91.      (dotlessi) [/i]
  92.      (oe) [/o /e]
  93.      (germandbls) [/s /s]
  94.    dicttomark /composites exch def
  95.  
  96. % Note the characters that must be defined as subroutines.
  97.    96 dict begin
  98.      0 composites
  99.       { exch pop
  100.          { dup currentdict exch known
  101.         { pop }
  102.         { 1 index def 1 add }
  103.        ifelse
  104.      }
  105.     forall
  106.       }
  107.      forall pop
  108.      currentdict
  109.    end /subrchars exch def
  110.  
  111. % Define the overstruck characters that can be synthesized with seac.
  112.    mark
  113.     [ /Aacute /Acircumflex /Adieresis /Agrave /Aring /Atilde
  114.       /Ccedilla
  115.       /Eacute /Ecircumflex /Edieresis /Egrave
  116.       /Iacute /Icircumflex /Idieresis /Igrave
  117.       /Ntilde
  118.       /Oacute /Ocircumflex /Odieresis /Ograve /Otilde
  119.       /Scaron
  120.       /Uacute /Ucircumflex /Udieresis /Ugrave
  121.       /Yacute /Ydieresis
  122.       /Zcaron
  123.       /aacute /acircumflex /adieresis /agrave /aring /atilde
  124.       /ccedilla
  125.       /eacute /ecircumflex /edieresis /egrave
  126.       /iacute /icircumflex /idieresis /igrave
  127.       /ntilde
  128.       /oacute /ocircumflex /odieresis /ograve /otilde
  129.       /scaron
  130.       /uacute /ucircumflex /udieresis /ugrave
  131.       /yacute /ydieresis
  132.       /zcaron
  133.     ]
  134.     { dup dup length string cvs
  135.       [ exch dup 0 1 getinterval
  136.         exch dup length 1 sub 1 exch getinterval
  137.       ]
  138.     } forall
  139.      /cent [/c /slash]
  140.      /sterling [/L /hyphen]
  141.      /yen [/Y /equal]
  142.      /daggerdbl [/bar /equal]
  143.    dicttomark /accentedchars exch def
  144.  
  145. % ------ BDF file parsing utilities ------ %
  146.  
  147. % Define a buffer for reading the BDF file.
  148.    /buffer 400 string def
  149.  
  150. % Read a line from the BDF file into the buffer.
  151. % Define /keyword as the first word on the line.
  152. % Define /args as the remainder of the line.
  153. % If the keyword is equal to commentword, skip the line.
  154. % (If commentword is equal to a space, never skip.)
  155.    /nextline
  156.     { bdfile buffer readline not
  157.        { (Premature EOF\n) print stop } if
  158.       ( ) search
  159.        { /keyword exch def pop }
  160.        { /keyword exch def () }
  161.       ifelse
  162.       /args exch def
  163.       keyword commentword eq { nextline } if
  164.     } bind def
  165.  
  166. % Get a word argument from args.  We do *not* copy the string.
  167.    /warg        % warg -> string
  168.     { args ( ) search
  169.        { exch pop exch }
  170.        { () }
  171.       ifelse  /args exch def
  172.     } bind def
  173.  
  174. % Get an integer argument from args.
  175.    /iarg        % iarg -> int
  176.     { warg cvi
  177.     } bind def
  178.  
  179. % Get a numeric argument from args.
  180.    /narg        % narg -> int|real
  181.     { warg cvr
  182.       dup dup cvi eq { cvi } if
  183.     } bind def
  184.  
  185. % Convert the remainder of args into a string.
  186.    /remarg        % remarg -> string
  187.     { args copystring
  188.     } bind def
  189.  
  190. % Get a string argument that occupies the remainder of args.
  191.    /sarg        % sarg -> string
  192.     { args (") anchorsearch
  193.        { pop /args exch def } { pop } ifelse
  194.       args args length 1 sub get (") 0 get eq
  195.        { args 0 args length 1 sub getinterval /args exch def } if
  196.       args copystring
  197.     } bind def
  198.  
  199. % Check that the keyword is the expected one.
  200.    /checkline        % (EXPECTED-KEYWORD) checkline ->
  201.     { dup keyword ne
  202.        { (Expected ) print =
  203.          (Line=) print keyword print ( ) print args print (\n) print stop
  204.        } if
  205.       pop
  206.     } bind def
  207.  
  208. % Read a line and check its keyword.
  209.    /getline        % (EXPECTED-KEYWORD) getline ->
  210.     { nextline checkline
  211.     } bind def
  212.  
  213. % Find the first/last non-zero bit of a non-zero byte.
  214.    /fnzb
  215.     { 0 { exch dup 128 ge { pop exit } { dup add exch 1 add } ifelse }
  216.       loop
  217.     } bind def
  218.    /lnzb
  219.     { 7 { exch dup 1 and 0 ne { pop exit } { -1 bitshift exch 1 sub } ifelse }
  220.       loop
  221.     } bind def
  222.  
  223. % ------ Type 1 encoding utilities ------ %
  224.  
  225. % Parse the side bearing and width information that begins a CharString.
  226. % Arguments: charstring.  Result: mark sbx wx substring *or*
  227. % mark sbx sby wx wy substring.
  228.    /parsesbw
  229.     { mark exch lenIV
  230.        {        % stack: mark ... string dropcount
  231.          dup 2 index length exch sub getinterval
  232.      dup 0 get dup 32 lt { pop exit } if
  233.      dup 246 le
  234.       { 139 sub exch 1 }
  235.       { dup 250 le
  236.          { 247 sub 8 bitshift 108 add 1 index 1 get add exch 2 }
  237.          { dup 254 le
  238.             { 251 sub 8 bitshift 108 add 1 index 1 get add neg exch 2 }
  239.         { pop dup 1 get 128 xor 128 sub
  240.           8 bitshift 1 index 2 get add
  241.           8 bitshift 1 index 3 get add
  242.           8 bitshift 1 index 4 get add exch 5
  243.         } ifelse
  244.          } ifelse
  245.       } ifelse
  246.        } loop
  247.     } bind def 
  248.  
  249. % Find the side bearing and width information that begins a CharString.
  250. % Arguments: charstring.  Result: charstring sizethroughsbw.
  251.    /findsbw
  252.     { dup parsesbw counttomark 1 add 1 roll cleartomark skipsbw
  253.     } bind def
  254.    /skipsbw        % charstring sbwprefix -> sizethroughsbw
  255.     { length 1 index length exch sub
  256.       2 copy get 12 eq { 2 } { 1 } ifelse add
  257.     } bind def
  258.  
  259. % Encode a number, and append it to a string.
  260. % Arguments: str num.  Result: newstr.
  261.    /concatnum
  262.     { dup dup -107 ge exch 107 le and
  263.        { 139 add 1 string dup 0 3 index put }
  264.        { dup dup -1131 ge exch 1131 le and
  265.           { dup 0 ge { 16#f694 } { neg 16#fa94 } ifelse add
  266.         2 string dup 0 3 index -8 bitshift put
  267.         dup 1 3 index 255 and put
  268.       }
  269.       { 5 string dup 0 255 put exch
  270.         2 copy 1 exch -24 bitshift 255 and put
  271.         2 copy 2 exch -16 bitshift 255 and put
  272.         2 copy 3 exch -8 bitshift 255 and put
  273.         2 copy 4 exch 255 and put
  274.         exch
  275.       }
  276.      ifelse
  277.        }
  278.       ifelse exch pop concatstrings
  279.     } bind def
  280.  
  281. % Encode a subroutine call for a given character, appending it to a string.
  282. % Arguments: str subrindex.  Result: newstr.
  283.    /concatcall
  284.     { () exch concatnum
  285.       s_callsubr concatstrings concatstrings
  286.     } bind def
  287.  
  288. % ------ Point arithmetic utilities ------ %
  289.  
  290.    /ptadd { exch 4 -1 roll add 3 1 roll add } bind def
  291.  
  292.    /ptexch { 4 2 roll } bind def
  293.  
  294.    /ptneg { neg exch neg exch } bind def
  295.  
  296.    /ptsub { ptneg ptadd } bind def
  297.  
  298. % ------ The main program ------ %
  299.  
  300.    /readBDF        % infilename outfilename fontname encodingname
  301.             %   uniqueID readBDF -> font
  302.     { /uniqueID exch def
  303.       /encoding exch def
  304.       /fontname exch def
  305.       /psname exch def
  306.       /bdfname exch def
  307.       gsave        % so we can set the CTM to the font matrix
  308.  
  309. %  Open the input files.  We don't open the output file until
  310. %  we've done a minimal validity check on the input.
  311.       bdfname (r) file /bdfile exch def
  312.       /commentword ( ) def
  313.  
  314. %  Check for the STARTFONT.
  315.       (STARTFONT) getline
  316.       args (2.1) ne { (Not version 2.1\n) print stop } if
  317.  
  318. %  Initialize the font.
  319.       /Font 20 dict def
  320.       Font begin
  321.       /FontName fontname def
  322.       /PaintType 0 def
  323.       /FontType 1 def
  324.       /UniqueID uniqueID def
  325.       /Encoding encoding cvx exec def
  326.       /FontInfo 20 dict def
  327.       /Private 20 dict def
  328.       currentdict end currentdict end
  329.       exch begin begin        % insert font above environment
  330.  
  331. %  Initialize the Private dictionary in the font.
  332.       Private begin
  333.       /-! {string currentfile exch readhexstring pop} readonly def
  334.       /-| {string currentfile exch readstring pop} readonly def
  335.       /|- {readonly def} readonly def
  336.       /| {readonly put} readonly def
  337.       /BlueValues [] def
  338.       /lenIV lenIV def
  339.       /MinFeature {16 16} def
  340.       /password 5839 def
  341.       /UniqueID uniqueID def
  342.       end        % Private
  343.  
  344. %  Now open the output file.
  345.       psname (w) file /psfile exch def
  346.  
  347. %  Put out a header compatible with the Adobe "standard".
  348.       (%!FontType1-1.0: ) ws fontname wt (000.000) wl
  349.       (% This is a font description converted from ) ws
  350.         bdfname wl
  351.       (%   by bdftops running on revision ) ws
  352.       revision wt (of (a) ) ws
  353.       statusdict /product get ws (.) wl
  354.  
  355. %  Copy the initial comments, up to FONT.
  356.       true
  357.       { nextline
  358.         keyword (COMMENT) ne {exit} if
  359.     { (% Here are the initial comments from the BDF file:\n%) wl
  360.     } if false
  361.     (%) ws remarg wl
  362.       } loop pop
  363.       /commentword (COMMENT) def    % do skip comments from now on
  364.  
  365. %  Read and process the FONT, SIZE, and FONTBOUNDINGBOX.
  366.       % If we cared about FONT, we'd use it here.  If the BDF files
  367.       % from MIT had PostScript names rather than X names, we would
  368.       % care; but what's there is unusable, so we discard FONT.
  369.       (FONT) checkline
  370.       (SIZE) getline
  371.         /pointsize iarg def   /xres iarg def   /yres iarg def
  372.       (FONTBOUNDINGBOX) getline
  373.         /fbbw iarg def   /fbbh iarg def   /fbbxo iarg def   /fbbyo iarg def
  374.     /fraster fbbw 7 add 8 idiv def
  375.       nextline
  376.  
  377. % Allocate the buffers for the bitmap and the outline,
  378. % according to the font bounding box.
  379.       /bits fraster fbbh mul 200 max 65535 min string def
  380.       /outline bits length 6 mul 65535 min string def
  381.  
  382. %  The Type 1 font machinery really only works with a 1000 unit
  383. %  character coordinate system.  Set this up here.
  384.  
  385. % Compute the factor to make the X entry in the FontMatrix
  386. % come out at exactly 0.001.
  387.       /fontscale   72 pointsize div xres div 1000 mul   def
  388.       Font /FontBBox
  389.        [ fbbxo fontscale mul
  390.      fbbyo fontscale mul
  391.      fbbxo fbbw add fontscale mul
  392.      fbbyo fbbh add fontscale mul
  393.        ] cvx readonly
  394.       put
  395.  
  396. %  Read and process the properties.  We only care about a few of them.
  397.       keyword (STARTPROPERTIES) eq
  398.        { iarg
  399.           { nextline
  400.         properties keyword known
  401.          { FontInfo properties keyword get sarg readonly put
  402.          } if
  403.       } repeat
  404.          (ENDPROPERTIES) getline
  405.      nextline
  406.        } if
  407.  
  408. %  Compute and set the FontMatrix.
  409.       Font /FontMatrix
  410.        [ 0.001 0 0 0.001 xres mul yres div 0 0 ] readonly
  411.       dup setmatrix put
  412.  
  413. %  Read and process the header for the bitmaps.
  414.       (CHARS) checkline
  415.         /ccount iarg def
  416.  
  417. %  Initialize the character subroutine table and the CharStrings dictionary.
  418.       /subrs subrchars length array def
  419.       /subrsbw subrchars length array def
  420.       /subrcount 0 def
  421.       /charstrings ccount composites length add
  422.         accentedchars length add 1 add dict def        % 1 add for .notdef
  423.       /isfixedwidth true def
  424.       /fixedwidth null def
  425.  
  426. %  Read and process the bitmap data.  This reads the remainder of the file.
  427.       ccount -1 1
  428.        { (STARTCHAR) getline
  429.            /charname remarg def
  430.      (/) print charname print
  431.        10 mod 1 eq { (\n) print flush } if
  432.      (ENCODING) getline        % Ignore, assume StandardEncoding
  433.      (SWIDTH) getline
  434.        /swx iarg pointsize mul 1000 div xres mul 72 div def
  435.        /swy iarg pointsize mul 1000 div xres mul 72 div def
  436.      (DWIDTH) getline        % Ignore, use SWIDTH instead
  437.      (BBX) getline
  438.        /bbw iarg def  /bbh iarg def  /bbox iarg def  /bboy iarg def
  439.      nextline
  440.      keyword (ATTRIBUTES) eq
  441.       { nextline
  442.       } if
  443.      (BITMAP) checkline
  444.  
  445. %  Read the bits for this character.
  446.      bbw 7 add 8 idiv /raster exch def
  447. % The bitmap handed to type1imagepath must have the correct height,
  448. % because type1imagepath uses this to compute the scale factor,
  449. % so we have to clear the unused parts of it.
  450.      bits dup 0 1 raster fbbh mul 1 sub
  451.       { 0 put dup } for
  452.      pop pop
  453.      raster fbbh bbh sub mul   raster   raster fbbh 1 sub mul
  454.       { bits exch raster getinterval
  455.         bdfile buffer readline not
  456.          { (EOF in bitmap\n) print stop } if
  457.         % stack has <bits.interval> <buffer.interval>
  458.         0 () /SubFileDecode filter
  459.         exch 2 copy readhexstring pop pop pop closefile
  460.       } for
  461.      (ENDCHAR) getline
  462.  
  463. %  Compute the font entry, converting the bitmap to an outline.
  464.      bits 0 raster fbbh mul getinterval    % the bitmap image
  465.      bbw   fbbh                % bitmap width & height
  466.      swx   swy                % width x & y
  467.      bbox neg   bboy neg            % origin x & y
  468.          % Account for lenIV when converting the outline.
  469.      outline  lenIV  outline length lenIV sub  getinterval
  470.      type1imagepath
  471.      length lenIV add
  472.      outline exch 0 exch getinterval
  473.  
  474. % Check for a fixed width font.
  475.      isfixedwidth
  476.       { fixedwidth null eq
  477.          { /fixedwidth swx def }
  478.          { fixedwidth swx ne { /isfixedwidth false def } if }
  479.         ifelse
  480.       } if
  481.  
  482. % Check whether this character must be a subroutine.
  483. % If so, strip off the initial [h]sbw, replace the endchar by a return,
  484. % and put the charstring in the Subrs array.
  485.      subrchars charname known
  486.       { /charstr exch def
  487.         /csindex subrchars charname get def
  488.         charstr parsesbw counttomark 1 add 1 roll
  489.           counttomark 2 eq { 0 exch 0 } if ]
  490.           subrsbw exch csindex exch put
  491.           charstr exch skipsbw /charend exch def pop
  492.         charstr charstr length 1 sub c_return put
  493.         subrs   csindex
  494.           charstr   charend lenIV sub   dup charstr length exch sub
  495.             getinterval copystring
  496.         put
  497.         charstr 0 charend getinterval
  498.           () subrchars charname get concatcall s_endchar concatstrings
  499.           concatstrings
  500.         /subrcount subrcount 1 add def
  501.       }
  502.       { copystring }
  503.      ifelse
  504.      charname exch charstrings 3 1 roll put
  505.        } for
  506.       (ENDFONT) getline
  507.  
  508. %  Synthesize missing characters out of available ones.
  509. %  For fixed-width fonts, only do this in the 1-for-1 case.
  510.       composites
  511.        { 1 index charstrings exch known
  512.           { pop pop }
  513.       { dup isfixedwidth
  514.          { dup length 1 eq }
  515.          { true }
  516.         ifelse
  517.         exch { charstrings exch known and } forall
  518.          { ( /) print 1 index bits cvs print
  519.            dup length 1 eq
  520.             { 0 get charstrings exch get copystring }
  521.         { % Top of stack is array of characters to combine.
  522.           % Convert to an array of subr indices.
  523.           [ exch { subrchars exch get } forall ]
  524.           % The final width is the sum of the widths of all
  525.           % the characters, minus the side bearings of all the
  526.           % characters except the first.  After each character
  527.           % except the last, do a setcurrentpoint of its width
  528.           % minus its side bearing (except for the first character);
  529.           % before each character except the first, do a 0 hmoveto.
  530.           % Fortunately, all this information is available in subrsbw.
  531.           /combine exch def
  532.           lenIV string
  533.           % Compute the total width.
  534.           subrsbw combine 0 get get aload pop pop pop 2 copy
  535.           combine
  536.            { subrsbw exch get
  537.              aload pop ptexch ptsub ptadd
  538.            } forall
  539.           % Encode the combined side bearing and width.
  540.           dup 3 index or 0 eq
  541.            { pop exch pop 2 array astore s_hsbw }
  542.            { 4 array astore s_sbw }
  543.           ifelse
  544.           3 1 roll { concatnum } forall exch concatstrings
  545.           % Encode the subroutine calls, except the last.
  546.           subrsbw combine 0 get get aload pop ptexch pop pop
  547.           0 1 combine length 2 sub
  548.            { combine exch get /ccsi exch def
  549.              2 copy 5 -1 roll ccsi concatcall
  550.              3 -1 roll concatnum exch concatnum
  551.              s_setcurrentpoint_hmoveto concatstrings
  552.              subrsbw ccsi get aload pop ptexch ptsub
  553.              5 -2 roll ptadd
  554.            } for
  555.           % Encode the last call.
  556.           pop pop
  557.           combine dup length 1 sub get concatcall
  558.           s_endchar concatstrings
  559.         } ifelse
  560.            charstrings 3 1 roll put
  561.          }
  562.          { pop pop }
  563.         ifelse
  564.       }
  565.      ifelse
  566.        }
  567.       forall flush
  568.  
  569. %  Synthesize accented characters with seac if needed and possible.
  570.       accentedchars
  571.        { aload pop /accent exch def /base exch def
  572.          buffer cvs /accented exch def
  573.      charstrings accented known not
  574.      charstrings base known and
  575.      charstrings accent known and
  576.       { ( /) print accented print
  577.         charstrings base get findsbw 0 exch getinterval
  578.         /acstring exch def        % start with sbw of base
  579.         charstrings accent get parsesbw
  580.         counttomark 1 sub { pop } repeat    % just leave mark & sbx
  581.         acstring exch concatnum exch pop    % pop the mark
  582.         0 concatnum 0 concatnum        % adx ady
  583.         decoding base get concatnum        % bchar
  584.         decoding accent get concatnum    % achar
  585.         s_seac concatstrings
  586.         charstrings exch accented copystring exch put
  587.       } if
  588.        } forall
  589.  
  590. %  Make a CharStrings entry for .notdef.
  591.       outline lenIV <8b8b0d0e> putinterval    % 0 0 hsbw endchar
  592.       charstrings /.notdef outline 0 lenIV 4 add getinterval copystring put
  593.  
  594. %  Encrypt the CharStrings and Subrs (in place).
  595.       charstrings dup begin
  596.        { 4330 exch dup type1encrypt exch pop
  597.          readonly def
  598.        }
  599.       forall end
  600.       0 1 subrs length 1 sub
  601.        { dup subrs exch get dup null ne
  602.       { 4330 exch dup type1encrypt exch pop
  603.         subrs 3 1 roll put
  604.       }
  605.       { pop pop }
  606.      ifelse
  607.        }
  608.       for
  609.  
  610. %  Make most of the remaining entries in the font dictionaries.
  611.       Font /CharStrings charstrings readonly put
  612.       FontInfo /FullName known not
  613.        { % Some programs insist on FullName being present.
  614.          FontInfo /FullName FontName dup length string cvs put
  615.        }
  616.       if
  617.       FontInfo /isFixedPitch isfixedwidth put
  618.       subrcount 0 gt
  619.        { Private /Subrs subrs readonly put
  620.        } if
  621.  
  622. %  Determine the italic angle and underline position
  623. %  by actually installing the font.
  624.       save
  625.       /_temp_ Font definefont setfont
  626.       [1000 0 0 1000 0 0] setmatrix        % mitigate rounding problems
  627. % The italic angle is the multiple of -5 degrees
  628. % that minimizes the width of the 'I'.
  629.       0 9999 0 5 85
  630.        { dup rotate
  631.          newpath 0 0 moveto (I) false charpath
  632.      dup neg rotate
  633.          pathbbox pop exch pop exch sub
  634.      dup 3 index lt { 4 -2 roll } if
  635.      pop pop
  636.        }
  637.       for pop
  638. % The underline position is halfway between the bottom of the 'A'
  639. % and the bottom of the FontBBox.
  640.       newpath 0 0 moveto (A) false charpath
  641.       FontMatrix concat
  642.       pathbbox pop pop exch pop
  643. %  Put the values in FontInfo.
  644.       3 -1 roll restore
  645.       Font /FontBBox get 1 get add 2 div cvi
  646.       dup FontInfo /UnderlinePosition 3 -1 roll put
  647.       2 div abs FontInfo /UnderlineThickness 3 -1 roll put
  648.       FontInfo /ItalicAngle 3 -1 roll put
  649.  
  650. %  Clean up and finish.
  651.       grestore
  652.       bdfile closefile
  653.       Font currentdict end end begin        % remove font from dict stack
  654.       (\n) print flush
  655.  
  656.     } bind def
  657.  
  658. % ------ Reader for AFM files ------ %
  659.  
  660. % Dictionary for looking up character keywords
  661.    /cmdict 6 dict dup begin
  662.       /C { /c iarg def } def
  663.       /N { /n warg copystring def } def
  664.       /WX { /w narg def } def
  665.       /W0X /WX load def
  666.       /W /WX load def
  667.       /W0 /WX load def
  668.    end def
  669.  
  670.    /readAFM        % fontdict afmfilename readAFM -> fontdict
  671.     { (r) file /bdfile exch def
  672.       /Font exch def
  673.       /commentword (Comment) def
  674.  
  675. %  Check for the StartFontMetrics.
  676.       (StartFontMetrics) getline
  677.       args cvr 2.0 lt { (Not version 2.0 or greater\n) print stop } if
  678.  
  679. %  Look for StartCharMetrics, then parse the character metrics.
  680. %  The only information we care about is the X width.
  681.       /metrics 0 dict def
  682.        { nextline
  683.          keyword (EndFontMetrics) eq { exit } if
  684.      keyword (StartCharMetrics) eq
  685.       { iarg dup dict /metrics exch def
  686.          { /c -1 def /n null def /w null def
  687.            nextline buffer
  688.             { token not { exit } if
  689.           dup cmdict exch known
  690.            { exch /args exch def   cmdict exch get exec   args }
  691.            { pop }
  692.           ifelse
  693.         } loop
  694.            c 0 ge n null ne or w null ne and
  695.             { n null eq { /n Font /Encoding get c get def } if
  696.           metrics n w put
  697.         }
  698.            if
  699.          }
  700.         repeat
  701.         (EndCharMetrics) getline
  702.       } if
  703.        } loop
  704.  
  705. %  Insert the metrics in the font.
  706.        metrics length 0 ne
  707.         { Font /Metrics metrics readonly put
  708.     } if
  709.       Font
  710.     } bind def
  711.  
  712. end        % envBDF
  713.  
  714. % Enter the main program in the current dictionary.
  715. /bdftops
  716.  { [] exch bdfafmtops
  717.  } bind def
  718. /bdfafmtops        % infilename afmfilename* outfilename fontname
  719.             %   encoding uniqueID
  720.  { envBDF begin
  721.      6 -2 roll exch 6 2 roll    % afm* in out fontname encoding uniqueID
  722.      readBDF        % afm* font
  723.      exch { readAFM } forall
  724.      save exch
  725.      dup /FontName get exch definefont
  726.      setfont
  727.      psfile writefont
  728.      restore
  729.      psfile closefile
  730.    end
  731.  } bind def
  732.  
  733. % If the program was invoked from the command line, run it now.
  734. [ shellarguments
  735.  { counttomark 4 ge
  736.     { dup 0 get
  737.       dup 48 ge exch 57 le and        % last arg starts with a digit?
  738.        { cvi /StandardEncoding }        % no encodingname
  739.        { cvn exch cvi exch }            % have encodingname
  740.       ifelse
  741.       counttomark 4 roll
  742.       counttomark 5 sub array astore
  743.       6 -4 roll exch
  744.       bdfafmtops
  745.     }
  746.     { cleartomark
  747.       (Usage: bdftops xx.bdf [yy1.afm ...] zz.gsf fontname uniqueID [encodingname]\n) print flush
  748.       mark
  749.     }
  750.    ifelse
  751.  }
  752. if pop
  753.